package com.supermarket.manage.business.product.controller;

import com.supermarket.manage.common.dto.CodeStatus;
import com.supermarket.manage.common.dto.PageBean;
import com.supermarket.manage.common.dto.ResponseResult;
import com.supermarket.manage.common.dto.prod.request.ProdPageParams;
import com.supermarket.manage.common.dto.prod.response.ProductDto;
import com.supermarket.manage.common.pojo.Product;
import com.supermarket.manage.common.utils.BigDecimalUtil;
import com.supermarket.manage.common.validation.CheckGroup;
import com.supermarket.manage.provider.api.product.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-08 19:23
 **/
@RestController
@RequestMapping("/prod")
@Api(value = "ProductController", tags = "商品管理接口")
public class ProductController {

    @Reference(version = "1.0.0")
    private ProductService productService;

    @PreAuthorize("hasAuthority('/logined')")
    @PostMapping("/getProdPageList")
    @ApiOperation(value = "条件查询商品分页列表", notes = "条件查询商品分页列表")
    public ResponseResult<PageBean<ProductDto>> getProdPageList(@Validated({CheckGroup.Add.class})
                                                                @RequestBody ProdPageParams params) {
        PageBean<ProductDto> result = productService.getProdPageList(params);
        if (result == null) {
            return new ResponseResult<>(CodeStatus.FAIL, "查询失败");
        }
        return new ResponseResult<>(CodeStatus.OK, "查询成功", result);
    }

    @PreAuthorize("hasAuthority('/logined')")
    @GetMapping("/getProdById/{pid}")
    @ApiOperation(value = "根据id获取商品信息", notes = "根据id获取商品信息")
    public ResponseResult<ProductDto> getProdById(@NotBlank(message = "商品id不能为空")
                                                  @PathVariable String pid) {
        ProductDto product = productService.getProdDtoByPid(pid);
        if (product == null) {
            return new ResponseResult<>(CodeStatus.FAIL, "查询失败，商品不存在");
        }
        return new ResponseResult<>(CodeStatus.OK, "查询成功", product);
    }

    @PreAuthorize("hasAnyAuthority('/','/product/manage')")
    @PostMapping("/add")
    @ApiOperation(value = "添加商品", notes = "添加商品")
    public ResponseResult<Void> addProduct(@Validated({CheckGroup.Add.class})
                                           @RequestBody Product product) {
        Integer count = productService.getCountByPname(product);
        if (count > 0) {
            return new ResponseResult<>(CodeStatus.FAIL, "添加失败，商品名称重复");
        }
        //精确精度
        product.setCostPrice(BigDecimalUtil.formatTwoPoint(product.getCostPrice()));
        product.setSalePrice(BigDecimalUtil.formatTwoPoint(product.getSalePrice()));
        product.setVipPrice(BigDecimalUtil.formatTwoPoint(product.getVipPrice()));
        Boolean aBoolean = productService.addProduct(product);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "添加成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "添加失败");
    }

    @PreAuthorize("hasAnyAuthority('/','/product/manage')")
    @GetMapping("/deleteProdByPid/{pid}")
    @ApiOperation(value = "下架商品", notes = "下架商品")
    public ResponseResult<Void> deleteProdByPid(@NotBlank(message = "商品id不能为空")
                                                @PathVariable String pid) {
        Boolean aBoolean = productService.deleteProdByPid(pid);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "删除成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "删除失败");
    }

    @PreAuthorize("hasAnyAuthority('/','/product/manage')")
    @PostMapping("/updateProd")
    @ApiOperation(value = "更新商品", notes = "更新商品")
    public ResponseResult<Void> updateProd(@Validated({CheckGroup.Update.class})
                                           @RequestBody Product product) {
        Product oldProd = productService.getProdByPid(product.getPid());
        if (oldProd == null) {
            return new ResponseResult<>(CodeStatus.FAIL, "更新失败，商品不存在");
        }
        if (!oldProd.getPname().equals(product.getPname())) {
            if (productService.getCountByPname(product) > 0) {
                return new ResponseResult<>(CodeStatus.FAIL, "更新失败，商品名称重复");
            }
        }
        product.setCostPrice(BigDecimalUtil.formatTwoPoint(product.getCostPrice()));
        product.setSalePrice(BigDecimalUtil.formatTwoPoint(product.getSalePrice()));
        product.setVipPrice(BigDecimalUtil.formatTwoPoint(product.getVipPrice()));
        Boolean aBoolean = productService.updateProd(oldProd, product);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "更新成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "更新失败");
    }
}
